package com.kkb.mybatis.framework.sqlnode;

import com.kkb.mybatis.framework.config.DynamicContext;
import com.kkb.mybatis.utils.OgnlUtils;

public class IfSqlNode implements SqlNode {

    private String test;

    private SqlNode mixedSqlnode;

    public IfSqlNode(String test, SqlNode mixedSqlnode) {
        this.test = test;
        this.mixedSqlnode = mixedSqlnode;
    }

    @Override
    public void apply(DynamicContext context) {
        //使用ognl对test表达式进行判断
        Object parameter = context.getBindings().get("_parameter");
        boolean flag = OgnlUtils.evaluateBoolean(test, parameter);
        //条件成立 appendSql
        if (flag) {
            mixedSqlnode.apply(context);
        }
    }
}
